<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="视频名称" prop="videoName">
        <el-input
          v-model="queryParams.videoName"
          placeholder="请输入视频名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否上线" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择视频状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标点名称" prop="siteName">
        <el-select v-model="queryParams.siteId" placeholder="请选择标点名称" @change="getSiteName" clearable filterable>
          <el-option v-for="dict in siteList" :key="dict.value" :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['video:video:add']">
          新增
        </el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange" border>
      <div slot="empty">
        <el-empty description="暂无数据"></el-empty>
      </div>
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="视频ID" align="center" prop="videoId"/>-->
      <el-table-column label="标点名称" width="180" show-overflow-tooltip align="center" prop="siteName"/>
      <el-table-column label="视频名称" width="180" show-overflow-tooltip align="center" prop="videoName"/>
      <!--      <el-table-column label="视频链接" width="200"  align="center" show-overflow-tooltip prop="videoUrl"/>-->
      <el-table-column label="视频详情" align="center" show-overflow-tooltip prop="videoUrl">
        <template slot-scope="scope">
          <div class="video-detail" @click="gotoDetail(scope.row)">
            <i class="el-icon-video-camera"/> {{ scope.row.videoUrl }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否上线" width="150" show-overflow-tooltip align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="结束图片" width="150" show-overflow-tooltip align="center" prop="endImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.endImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="坐标点ID" align="center" prop="siteId"/>-->
      <!--      <el-table-column label="标点名称" align="center" prop="siteName"/>-->
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="waring"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['video:video:edit']"
          >修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['video:video:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10">
          <el-col :sm="24" :lg="14">
            <el-form-item label="视频名称" prop="videoName">
              <el-input v-model="form.videoName" placeholder="请输入视频名称"/>
            </el-form-item>
            <el-form-item label="视频文件" prop="videoUrl">
              <file-upload v-model="form.videoUrl" :limit="1"/>
            </el-form-item>
            <el-form-item label="是否上线" prop="state">
              <el-select v-model="form.state" placeholder="请选择视频状态">
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="所属标点" prop="siteId">
              <!--              <el-input v-model="form.siteName" placeholder="请输入标点名称"/>-->
              <el-select v-model="form.siteId" placeholder="请选择视频状态" @change="getSiteName">
                <el-option v-for="dict in siteList" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :lg="10">
            <el-form-item label="结束图片" prop="endImg">
              <image-upload v-model="form.endImg" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listVideo, getVideo, delVideo, addVideo, updateVideo} from "@/api/applet/video";
import {getSiteSelectList} from "@/api/applet/appletSite";

export default {
  name: "Video",
  dicts: ['sys_yes_no'],
  data() {
    return {
      siteList: [],//标点
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频表格数据
      videoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        videoName: null,
        videoUrl: null,
        state: null,
        endImg: null,
        siteId: null,
        siteName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        videoName: [
          {required: true, message: "视频名称不能为空", trigger: "blur"}
        ],
        videoUrl: [
          {required: true, message: "视频链接不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "视频状态不能为空", trigger: "change"}
        ],
        createBy: [
          {required: true, message: "创建账号不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateBy: [
          {required: true, message: "更新账号不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ],
        endImg: [
          {required: true, message: "结束图片不能为空", trigger: "blur"}
        ],
        siteId: [
          {required: true, message: "坐标点不能为空", trigger: "change"}
        ],
        siteName: [
          {required: true, message: "标点名称不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getSiteList();
  },
  methods: {
    gotoDetail(row) {
      window.open(row.videoUrl)
    },


    getSiteName() {
      for (let i = 0; i < this.siteList.length; i++) {
        let obj = this.siteList[i];
        if (this.form.siteId == obj.value) {
          this.form.siteName = obj.label;
          break
        }
      }
    },

    //获取路线坐标图
    getSiteList() {
      getSiteSelectList().then(res => {
        this.siteList = res.data;
      })
    },
    /** 查询视频列表 */
    getList() {
      this.loading = true;
      listVideo(this.queryParams).then(response => {
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        videoId: null,
        videoName: null,
        videoUrl: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        endImg: null,
        siteId: null,
        siteName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.videoId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const videoId = row.videoId || this.ids
      getVideo(videoId).then(response => {
        this.form = response.data;
        this.form.siteId = this.form.siteId.toString()
        this.open = true;
        this.title = "修改视频";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.videoId != null) {
            updateVideo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const videoIds = row.videoId || this.ids;
      this.$modal.confirm('是否确认删除视频编号为"' + videoIds + '"的数据项？').then(function () {
        return delVideo(videoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('video/video/export', {
        ...this.queryParams
      }, `video_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.video-detail {
  color: #00afff;
  cursor: pointer;
}

.video-detail:hover {
  color: #1ab394;
  font-size: 15px;
}


</style>
